option (ENABLE_ODBC "Enable ODBC library" ${ENABLE_LIBRARIES})
if (NOT OS_LINUX OR USE_MUSL)
    if (ENABLE_ODBC)
        message(STATUS "ODBC is only supported on Linux with dynamic linking")
    endif()
    set (ENABLE_ODBC OFF CACHE INTERNAL "")
endif ()

if (NOT ENABLE_ODBC)
    message(STATUS "Not using ODBC")
    return()
endif()

set (LIBRARY_DIR "${ClickHouse_SOURCE_DIR}/contrib/unixodbc")

# ltdl

set (SRCS_LTDL
    # This file is generated by 'libtool' inside libltdl directory and then removed.
    linux_x86_64/libltdl/libltdlcS.c

    "${LIBRARY_DIR}/libltdl/lt__alloc.c"
    "${LIBRARY_DIR}/libltdl/lt__strl.c"
    "${LIBRARY_DIR}/libltdl/ltdl.c"
    "${LIBRARY_DIR}/libltdl/lt_dlloader.c"
    "${LIBRARY_DIR}/libltdl/slist.c"
    "${LIBRARY_DIR}/libltdl/lt_error.c"
    "${LIBRARY_DIR}/libltdl/loaders/dlopen.c"
    "${LIBRARY_DIR}/libltdl/loaders/preopen.c"
)

add_library (_ltdl ${SRCS_LTDL})

target_include_directories(_ltdl
    SYSTEM
    PRIVATE
        linux_x86_64/libltdl
    PUBLIC
        "${LIBRARY_DIR}/libltdl"
        "${LIBRARY_DIR}/libltdl/libltdl"
)
target_compile_definitions(_ltdl PRIVATE -DHAVE_CONFIG_H -DLTDL -DLTDLOPEN=libltdlc)
target_compile_options(_ltdl PRIVATE -Wno-constant-logical-operand -Wno-unknown-warning-option -O2)

# odbc

set (SRCS
    "${LIBRARY_DIR}/DriverManager/__attribute.c"
    "${LIBRARY_DIR}/DriverManager/__connection.c"
    "${LIBRARY_DIR}/DriverManager/__handles.c"
    "${LIBRARY_DIR}/DriverManager/__info.c"
    "${LIBRARY_DIR}/DriverManager/__stats.c"
    "${LIBRARY_DIR}/DriverManager/SQLAllocConnect.c"
    "${LIBRARY_DIR}/DriverManager/SQLAllocEnv.c"
    "${LIBRARY_DIR}/DriverManager/SQLAllocHandle.c"
    "${LIBRARY_DIR}/DriverManager/SQLAllocHandleStd.c"
    "${LIBRARY_DIR}/DriverManager/SQLAllocStmt.c"
    "${LIBRARY_DIR}/DriverManager/SQLBindCol.c"
    "${LIBRARY_DIR}/DriverManager/SQLBindParam.c"
    "${LIBRARY_DIR}/DriverManager/SQLBindParameter.c"
    "${LIBRARY_DIR}/DriverManager/SQLBrowseConnect.c"
    "${LIBRARY_DIR}/DriverManager/SQLBrowseConnectW.c"
    "${LIBRARY_DIR}/DriverManager/SQLBulkOperations.c"
    "${LIBRARY_DIR}/DriverManager/SQLCancel.c"
    "${LIBRARY_DIR}/DriverManager/SQLCancelHandle.c"
    "${LIBRARY_DIR}/DriverManager/SQLCloseCursor.c"
    "${LIBRARY_DIR}/DriverManager/SQLColAttribute.c"
    "${LIBRARY_DIR}/DriverManager/SQLColAttributes.c"
    "${LIBRARY_DIR}/DriverManager/SQLColAttributesW.c"
    "${LIBRARY_DIR}/DriverManager/SQLColAttributeW.c"
    "${LIBRARY_DIR}/DriverManager/SQLColumnPrivileges.c"
    "${LIBRARY_DIR}/DriverManager/SQLColumnPrivilegesW.c"
    "${LIBRARY_DIR}/DriverManager/SQLColumns.c"
    "${LIBRARY_DIR}/DriverManager/SQLColumnsW.c"
    "${LIBRARY_DIR}/DriverManager/SQLConnect.c"
    "${LIBRARY_DIR}/DriverManager/SQLConnectW.c"
    "${LIBRARY_DIR}/DriverManager/SQLCopyDesc.c"
    "${LIBRARY_DIR}/DriverManager/SQLDataSources.c"
    "${LIBRARY_DIR}/DriverManager/SQLDataSourcesW.c"
    "${LIBRARY_DIR}/DriverManager/SQLDescribeCol.c"
    "${LIBRARY_DIR}/DriverManager/SQLDescribeColW.c"
    "${LIBRARY_DIR}/DriverManager/SQLDescribeParam.c"
    "${LIBRARY_DIR}/DriverManager/SQLDisconnect.c"
    "${LIBRARY_DIR}/DriverManager/SQLDriverConnect.c"
    "${LIBRARY_DIR}/DriverManager/SQLDriverConnectW.c"
    "${LIBRARY_DIR}/DriverManager/SQLDrivers.c"
    "${LIBRARY_DIR}/DriverManager/SQLDriversW.c"
    "${LIBRARY_DIR}/DriverManager/SQLEndTran.c"
    "${LIBRARY_DIR}/DriverManager/SQLError.c"
    "${LIBRARY_DIR}/DriverManager/SQLErrorW.c"
    "${LIBRARY_DIR}/DriverManager/SQLExecDirect.c"
    "${LIBRARY_DIR}/DriverManager/SQLExecDirectW.c"
    "${LIBRARY_DIR}/DriverManager/SQLExecute.c"
    "${LIBRARY_DIR}/DriverManager/SQLExtendedFetch.c"
    "${LIBRARY_DIR}/DriverManager/SQLFetch.c"
    "${LIBRARY_DIR}/DriverManager/SQLFetchScroll.c"
    "${LIBRARY_DIR}/DriverManager/SQLForeignKeys.c"
    "${LIBRARY_DIR}/DriverManager/SQLForeignKeysW.c"
    "${LIBRARY_DIR}/DriverManager/SQLFreeConnect.c"
    "${LIBRARY_DIR}/DriverManager/SQLFreeEnv.c"
    "${LIBRARY_DIR}/DriverManager/SQLFreeHandle.c"
    "${LIBRARY_DIR}/DriverManager/SQLFreeStmt.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetConnectAttr.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetConnectAttrW.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetConnectOption.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetConnectOptionW.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetCursorName.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetCursorNameW.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetData.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetDescField.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetDescFieldW.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetDescRec.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetDescRecW.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetDiagField.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetDiagFieldW.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetDiagRec.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetDiagRecW.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetEnvAttr.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetFunctions.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetInfo.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetInfoW.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetStmtAttr.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetStmtAttrW.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetStmtOption.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetTypeInfo.c"
    "${LIBRARY_DIR}/DriverManager/SQLGetTypeInfoW.c"
    "${LIBRARY_DIR}/DriverManager/SQLMoreResults.c"
    "${LIBRARY_DIR}/DriverManager/SQLNativeSql.c"
    "${LIBRARY_DIR}/DriverManager/SQLNativeSqlW.c"
    "${LIBRARY_DIR}/DriverManager/SQLNumParams.c"
    "${LIBRARY_DIR}/DriverManager/SQLNumResultCols.c"
    "${LIBRARY_DIR}/DriverManager/SQLParamData.c"
    "${LIBRARY_DIR}/DriverManager/SQLParamOptions.c"
    "${LIBRARY_DIR}/DriverManager/SQLPrepare.c"
    "${LIBRARY_DIR}/DriverManager/SQLPrepareW.c"
    "${LIBRARY_DIR}/DriverManager/SQLPrimaryKeys.c"
    "${LIBRARY_DIR}/DriverManager/SQLPrimaryKeysW.c"
    "${LIBRARY_DIR}/DriverManager/SQLProcedureColumns.c"
    "${LIBRARY_DIR}/DriverManager/SQLProcedureColumnsW.c"
    "${LIBRARY_DIR}/DriverManager/SQLProcedures.c"
    "${LIBRARY_DIR}/DriverManager/SQLProceduresW.c"
    "${LIBRARY_DIR}/DriverManager/SQLPutData.c"
    "${LIBRARY_DIR}/DriverManager/SQLRowCount.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetConnectAttr.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetConnectAttrW.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetConnectOption.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetConnectOptionW.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetCursorName.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetCursorNameW.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetDescField.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetDescFieldW.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetDescRec.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetEnvAttr.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetParam.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetPos.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetScrollOptions.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetStmtAttr.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetStmtAttrW.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetStmtOption.c"
    "${LIBRARY_DIR}/DriverManager/SQLSetStmtOptionW.c"
    "${LIBRARY_DIR}/DriverManager/SQLSpecialColumns.c"
    "${LIBRARY_DIR}/DriverManager/SQLSpecialColumnsW.c"
    "${LIBRARY_DIR}/DriverManager/SQLStatistics.c"
    "${LIBRARY_DIR}/DriverManager/SQLStatisticsW.c"
    "${LIBRARY_DIR}/DriverManager/SQLTablePrivileges.c"
    "${LIBRARY_DIR}/DriverManager/SQLTablePrivilegesW.c"
    "${LIBRARY_DIR}/DriverManager/SQLTables.c"
    "${LIBRARY_DIR}/DriverManager/SQLTablesW.c"
    "${LIBRARY_DIR}/DriverManager/SQLTransact.c"
    "${LIBRARY_DIR}/ini/_iniDump.c"
    "${LIBRARY_DIR}/ini/_iniObjectRead.c"
    "${LIBRARY_DIR}/ini/_iniPropertyRead.c"
    "${LIBRARY_DIR}/ini/_iniScanUntilObject.c"
    "${LIBRARY_DIR}/ini/iniAllTrim.c"
    "${LIBRARY_DIR}/ini/iniAppend.c"
    "${LIBRARY_DIR}/ini/iniClose.c"
    "${LIBRARY_DIR}/ini/iniCommit.c"
    "${LIBRARY_DIR}/ini/iniCursor.c"
    "${LIBRARY_DIR}/ini/iniDelete.c"
    "${LIBRARY_DIR}/ini/iniElement.c"
    "${LIBRARY_DIR}/ini/iniElementCount.c"
    "${LIBRARY_DIR}/ini/iniGetBookmark.c"
    "${LIBRARY_DIR}/ini/iniGotoBookmark.c"
    "${LIBRARY_DIR}/ini/iniObject.c"
    "${LIBRARY_DIR}/ini/iniObjectDelete.c"
    "${LIBRARY_DIR}/ini/iniObjectEOL.c"
    "${LIBRARY_DIR}/ini/iniObjectFirst.c"
    "${LIBRARY_DIR}/ini/iniObjectInsert.c"
    "${LIBRARY_DIR}/ini/iniObjectLast.c"
    "${LIBRARY_DIR}/ini/iniObjectNext.c"
    "${LIBRARY_DIR}/ini/iniObjectSeek.c"
    "${LIBRARY_DIR}/ini/iniObjectSeekSure.c"
    "${LIBRARY_DIR}/ini/iniObjectUpdate.c"
    "${LIBRARY_DIR}/ini/iniOpen.c"
    "${LIBRARY_DIR}/ini/iniProperty.c"
    "${LIBRARY_DIR}/ini/iniPropertyDelete.c"
    "${LIBRARY_DIR}/ini/iniPropertyEOL.c"
    "${LIBRARY_DIR}/ini/iniPropertyFirst.c"
    "${LIBRARY_DIR}/ini/iniPropertyInsert.c"
    "${LIBRARY_DIR}/ini/iniPropertyLast.c"
    "${LIBRARY_DIR}/ini/iniPropertyNext.c"
    "${LIBRARY_DIR}/ini/iniPropertySeek.c"
    "${LIBRARY_DIR}/ini/iniPropertySeekSure.c"
    "${LIBRARY_DIR}/ini/iniPropertyUpdate.c"
    "${LIBRARY_DIR}/ini/iniPropertyValue.c"
    "${LIBRARY_DIR}/ini/iniToUpper.c"
    "${LIBRARY_DIR}/ini/iniValue.c"
    "${LIBRARY_DIR}/log/_logFreeMsg.c"
    "${LIBRARY_DIR}/log/logClear.c"
    "${LIBRARY_DIR}/log/logClose.c"
    "${LIBRARY_DIR}/log/logOn.c"
    "${LIBRARY_DIR}/log/logOpen.c"
    "${LIBRARY_DIR}/log/logPeekMsg.c"
    "${LIBRARY_DIR}/log/logPopMsg.c"
    "${LIBRARY_DIR}/log/logPushMsg.c"
    "${LIBRARY_DIR}/lst/_lstAdjustCurrent.c"
    "${LIBRARY_DIR}/lst/_lstDump.c"
    "${LIBRARY_DIR}/lst/_lstFreeItem.c"
    "${LIBRARY_DIR}/lst/_lstNextValidItem.c"
    "${LIBRARY_DIR}/lst/_lstPrevValidItem.c"
    "${LIBRARY_DIR}/lst/_lstVisible.c"
    "${LIBRARY_DIR}/lst/lstAppend.c"
    "${LIBRARY_DIR}/lst/lstClose.c"
    "${LIBRARY_DIR}/lst/lstDelete.c"
    "${LIBRARY_DIR}/lst/lstEOL.c"
    "${LIBRARY_DIR}/lst/lstFirst.c"
    "${LIBRARY_DIR}/lst/lstGet.c"
    "${LIBRARY_DIR}/lst/lstGetBookMark.c"
    "${LIBRARY_DIR}/lst/lstGoto.c"
    "${LIBRARY_DIR}/lst/lstGotoBookMark.c"
    "${LIBRARY_DIR}/lst/lstInsert.c"
    "${LIBRARY_DIR}/lst/lstLast.c"
    "${LIBRARY_DIR}/lst/lstNext.c"
    "${LIBRARY_DIR}/lst/lstOpen.c"
    "${LIBRARY_DIR}/lst/lstOpenCursor.c"
    "${LIBRARY_DIR}/lst/lstPrev.c"
    "${LIBRARY_DIR}/lst/lstSeek.c"
    "${LIBRARY_DIR}/lst/lstSeekItem.c"
    "${LIBRARY_DIR}/lst/lstSet.c"
    "${LIBRARY_DIR}/lst/lstSetFreeFunc.c"
    "${LIBRARY_DIR}/odbcinst/_logging.c"
    "${LIBRARY_DIR}/odbcinst/_odbcinst_ConfigModeINI.c"
    "${LIBRARY_DIR}/odbcinst/_odbcinst_GetEntries.c"
    "${LIBRARY_DIR}/odbcinst/_odbcinst_GetSections.c"
    "${LIBRARY_DIR}/odbcinst/_odbcinst_SystemINI.c"
    "${LIBRARY_DIR}/odbcinst/_odbcinst_UserINI.c"
    "${LIBRARY_DIR}/odbcinst/_SQLDriverConnectPrompt.c"
    "${LIBRARY_DIR}/odbcinst/_SQLGetInstalledDrivers.c"
    "${LIBRARY_DIR}/odbcinst/_SQLWriteInstalledDrivers.c"
    "${LIBRARY_DIR}/odbcinst/ODBCINSTConstructProperties.c"
    "${LIBRARY_DIR}/odbcinst/ODBCINSTDestructProperties.c"
    "${LIBRARY_DIR}/odbcinst/ODBCINSTSetProperty.c"
    "${LIBRARY_DIR}/odbcinst/ODBCINSTValidateProperties.c"
    "${LIBRARY_DIR}/odbcinst/ODBCINSTValidateProperty.c"
    "${LIBRARY_DIR}/odbcinst/SQLConfigDataSource.c"
    "${LIBRARY_DIR}/odbcinst/SQLConfigDriver.c"
    "${LIBRARY_DIR}/odbcinst/SQLCreateDataSource.c"
    "${LIBRARY_DIR}/odbcinst/SQLGetAvailableDrivers.c"
    "${LIBRARY_DIR}/odbcinst/SQLGetConfigMode.c"
    "${LIBRARY_DIR}/odbcinst/SQLGetInstalledDrivers.c"
    "${LIBRARY_DIR}/odbcinst/SQLGetPrivateProfileString.c"
    "${LIBRARY_DIR}/odbcinst/SQLGetTranslator.c"
    "${LIBRARY_DIR}/odbcinst/SQLInstallDriverEx.c"
    "${LIBRARY_DIR}/odbcinst/SQLInstallDriverManager.c"
    "${LIBRARY_DIR}/odbcinst/SQLInstallerError.c"
    "${LIBRARY_DIR}/odbcinst/SQLInstallODBC.c"
    "${LIBRARY_DIR}/odbcinst/SQLInstallTranslatorEx.c"
    "${LIBRARY_DIR}/odbcinst/SQLManageDataSources.c"
    "${LIBRARY_DIR}/odbcinst/SQLPostInstallerError.c"
    "${LIBRARY_DIR}/odbcinst/SQLReadFileDSN.c"
    "${LIBRARY_DIR}/odbcinst/SQLRemoveDriver.c"
    "${LIBRARY_DIR}/odbcinst/SQLRemoveDriverManager.c"
    "${LIBRARY_DIR}/odbcinst/SQLRemoveDSNFromIni.c"
    "${LIBRARY_DIR}/odbcinst/SQLRemoveTranslator.c"
    "${LIBRARY_DIR}/odbcinst/SQLSetConfigMode.c"
    "${LIBRARY_DIR}/odbcinst/SQLValidDSN.c"
    "${LIBRARY_DIR}/odbcinst/SQLWriteDSNToIni.c"
    "${LIBRARY_DIR}/odbcinst/SQLWriteFileDSN.c"
    "${LIBRARY_DIR}/odbcinst/SQLWritePrivateProfileString.c"
)

add_library (_unixodbc ${SRCS})

target_link_libraries (_unixodbc PRIVATE _ltdl)

# SYSTEM_FILE_PATH was changed to /etc

target_include_directories (_unixodbc
    SYSTEM
    PRIVATE
        linux_x86_64/private
    PUBLIC
        linux_x86_64
        "${LIBRARY_DIR}/include"
)
target_compile_definitions (_unixodbc PRIVATE -DHAVE_CONFIG_H)
target_compile_options (_unixodbc PRIVATE -O2) # intended?

add_library (ch_contrib::unixodbc ALIAS _unixodbc)
